package com.dhcc.DRGsHosp.application.impl;

import java.util.List;
import java.util.Set;

import javax.inject.Named;

import org.dayatang.domain.EntityRepository;
import org.dayatang.domain.InstanceFactory;
import org.openkoala.koalacommons.resourceloader.util.StringUtils;
import org.springframework.transaction.annotation.Transactional;

import com.dhcc.DRGsHosp.application.QuotaAdjustApplication;
import com.dhcc.DRGsHosp.core.domain.QuotaAdjust;
import com.dhcc.DRGsHosp.infra.util.DrgsIndex;

@Named
@Transactional
public class QuotaAdjustApplicationImpl implements QuotaAdjustApplication {
	
	private EntityRepository repository;

	private EntityRepository getRepository() {
		if (repository == null) {
			repository = InstanceFactory.getInstance(
					EntityRepository.class, "repository");
		}
		return repository;
	}
	
	public QuotaAdjust getQuotaAdjust(Double id) {
		return QuotaAdjust.get(QuotaAdjust.class, id);
	}
	
	public void creatQuotaAdjust(QuotaAdjust quotaAdjust) {
		quotaAdjust.save();
	}
	/**
	 *  @Description：更新测算调整参数
	 *  @Author liuhongjun 
	 *  @see com.dhcc.DRGsHosp.application.QuotaAdjustApplication#updateQuotaAdjust(com.dhcc.DRGsHosp.core.domain.QuotaAdjust)
	 */
	public void updateQuotaAdjust(QuotaAdjust quotaAdjust) {
		quotaAdjust .save();
	}
	
	public void removeQuotaAdjust(QuotaAdjust quotaAdjust) {
		if(quotaAdjust != null){
			quotaAdjust.remove();
		}
	}
	
	public void removeQuotaAdjusts(Set<QuotaAdjust> quotaAdjusts) {
		for (QuotaAdjust quotaAdjust : quotaAdjusts) {
			quotaAdjust.remove();
		}
	}
	
	public List<QuotaAdjust> findAllQuotaAdjust() {
		return QuotaAdjust.findAll(QuotaAdjust.class);
	}
/**
 * 
 *  @Description：获取科室科室组的调整参数
 *  @Author LiuHongJun  
 *  @see com.dhcc.DRGsHosp.application.QuotaAdjustApplication#findQuotaAdjustBy(com.dhcc.DRGsHosp.core.domain.QuotaAdjust)
 */
	@Override
	public List<QuotaAdjust> findQuotaAdjustBy(QuotaAdjust quotaAdjust) {
		StringBuilder jpql = new StringBuilder(" select _quotaAdjust from QuotaAdjust _quotaAdjust where 1=1 ");
		if(StringUtils.hasLength(quotaAdjust.getParDepGroupCode())){
			jpql.append(" and _quotaAdjust.parDepGroupCode='"+quotaAdjust.getParDepGroupCode()+"' ");
		}
		if(StringUtils.hasLength(quotaAdjust.getDepCode())){
			jpql.append(" and _quotaAdjust.depCode='"+quotaAdjust.getDepCode()+"' ");
		}
		if(StringUtils.hasLength(quotaAdjust.getPlanYear())){
			jpql.append(" and _quotaAdjust.planYear='"+quotaAdjust.getPlanYear()+"' ");
		}
		if(StringUtils.hasLength(quotaAdjust.getRoleName())){
			jpql.append(" and _quotaAdjust.roleName='"+quotaAdjust.getRoleName()+"' ");
		}
		List<QuotaAdjust> list = getRepository().createJpqlQuery(jpql.toString()).list();
		return list;
	}
/**
 * 
 *  @Description：获取调整参数
 *  @Author liuhongjun 
 *  @see com.dhcc.DRGsHosp.application.QuotaAdjustApplication#getForeCastParamByDep(java.lang.String, java.lang.String)
 */
	@Override
	public List<QuotaAdjust> getForeCastParamByDep(String depCode, String year) {
		StringBuilder jpql = new StringBuilder(" select _quotaAdjust from QuotaAdjust _quotaAdjust where 1=1 ");
		jpql.append(" and _quotaAdjust.depCode='"+depCode+"' ");
		jpql.append(" and _quotaAdjust.planYear='"+year+"' ");
		jpql.append(" and _quotaAdjust.roleName ='"+DrgsIndex.getRoleCode()+"' ");
		jpql.append(" and _quotaAdjust.drgCode is null ");
		List<QuotaAdjust> list = getRepository().createJpqlQuery(jpql.toString()).list();
		if(list.size()>0){
			return list;
		}
		return null;
	}
	
	@Override
	public List<QuotaAdjust> getForeCastParamByDrgs(QuotaAdjust quotaAdjustParam, String year) {
		QuotaAdjust quotaAdjust = null;
		StringBuilder jpql = new StringBuilder(" select _quotaAdjust from QuotaAdjust _quotaAdjust where 1=1 ");
		if(StringUtils.hasLength(quotaAdjustParam.getDepCode()) && StringUtils.hasLength(quotaAdjustParam.getDrgCode())){
			jpql.append(" and _quotaAdjust.depCode='"+quotaAdjustParam.getDepCode()+"' ");
			jpql.append(" and _quotaAdjust.drgCode='"+quotaAdjustParam.getDrgCode()+"' ");
		}else{
			jpql.append(" and 1>2 ");
		}
		jpql.append(" and _quotaAdjust.planYear='"+year+"' ");
		List<QuotaAdjust> list = getRepository().createJpqlQuery(jpql.toString()).list();
		if(list.size()>0){
			return list;
		}else{
			jpql = new StringBuilder(" select _quotaAdjust from QuotaAdjust _quotaAdjust where 1=1 ");
			if(StringUtils.hasLength(quotaAdjustParam.getParDepGroupCode()) && StringUtils.hasLength(quotaAdjustParam.getDrgCode())){
				jpql.append(" and _quotaAdjust.parDepGroupCode='"+quotaAdjustParam.getParDepGroupCode()+"' ");
				jpql.append(" and _quotaAdjust.drgCode='"+quotaAdjustParam.getDrgCode()+"' ");
			}else{
				jpql.append(" and 1>2 ");
			}
			jpql.append(" and _quotaAdjust.planYear='"+year+"' ");
			list = getRepository().createJpqlQuery(jpql.toString()).list();
			if(list.size()>0){
				return list;
			}
		}
		return null;
	}
	
}
